package com.gitee.wsl.compose.ext.text.builder

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.TextUnit



fun AnnotatedString.Builder.text(text: String, style: SpanStyle = SpanStyle()) = withStyle(style = style) {
    append(text)
}

/**
 * Applies [FontWeight.Bold] to `this` string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.bold(text: String) = weight(text, FontWeight.Bold)

/**
 * Applies [FontStyle.Italic] to `this` string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.italic(text: String) = style(text, FontStyle.Italic)

/**
 * Strikes through this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.strike(text: String) = text(text,
    SpanStyle(textDecoration = TextDecoration.LineThrough)
)

/**
 * Adds an underline to this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.underline(text: String)= text(text,
    SpanStyle(textDecoration = TextDecoration.Underline)
)

/**
 * Adds text [decorations] provided to this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.decorate(
    text: String,
    vararg decorations: TextDecoration
) = text(text, SpanStyle(textDecoration = TextDecoration.combine(decorations.asList())))

/**
 * Applies absolute [size] to the text
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.size(text: String, size: TextUnit) = text(text,
    SpanStyle(fontSize = size)
)

/**
 * Sets the background [color] to this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.background(text: String, color: Color) = text(text,
    SpanStyle(background = color)
)

/**
 * Sets the foreground [color] of this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.color(text: String, color: Color) = text(text, SpanStyle(color = color))

/**
 * Sets the font [weight] for this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.weight(text: String, weight: FontWeight) = text(text,
    SpanStyle(fontWeight = weight)
)

/**
 * Applies a [style] [FontStyle] to this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.style(text: String, style: FontStyle) = text(text,
    SpanStyle(fontStyle = style)
)

/**
 * Adds a shadow to `this` string. The shadow has a [color], an [offset] and a [blurRadius]
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.shadow(
    text: String,
    color: Color,
    offset: Offset = Offset.Zero,
    blurRadius: Float = 0.0f
) = text(text, SpanStyle(shadow = Shadow(color, offset, blurRadius)))

/**
 * Changes the [fontFamily] of this string
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.font(text: String, fontFamily: FontFamily) = text(text,
    SpanStyle(fontFamily = fontFamily)
)

/**
 * Makes this string clickable. When clicked, the [onClick] lambda is invoked
 *
 * @return the [AnnotatedString] created
 */
fun AnnotatedString.Builder.clickable(text: String, onClick: () -> Unit) {
    pushLink(LinkAnnotation.Clickable("clickable") { onClick() })
    pushStyle(SpanStyle(textDecoration = TextDecoration.Underline))
    append(text)
    pop()
    pop()
}


